package com.yyge.common.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Desc 描述
 * @Author: admin
 * @Date: 2023/07/06/10:55
 */
@Component
public class SpringBeanFactoryUtil implements ApplicationContextAware {

    private static ApplicationContext  applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringBeanFactoryUtil.applicationContext=applicationContext;
    }

    /**
     * 根据名称和类型来获取对象
     * @param beanName
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T  getBean(String beanName,Class<T> tClass){
        return  applicationContext.getBean(beanName,tClass);
    }

    /**
     * 根据名称来获取对象
     * @param beanName
     * @return
     */
    public static  Object getBean(String beanName ){
        return  applicationContext.getBean(beanName);
    }

}